<?php


namespace mpend\modules\notice\models;

use common\base\Model;
use common\helpers\ArrayHelper;
use common\models\actor\Actor;
use common\models\actor\ActorTag;
use common\models\actor\Taglib;
use common\models\notice\NoticeReceive;
use mpend\modules\actor\controllers\TaglibController;
use Yii;

class MessageSearch extends NoticeReceive
{

    public $type;

    public $pagesize = false;

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            ['type', 'required']
        ]);
    }


    public function buildQuery()
    {
        $query = static::find()->active();

        if ($this->type == 'system') { // 系统通告
            $query->andWhere([
                'or',
                ['user_id' => $this->user_id],
                ['user_id' => 0],
            ]);
            $query->andWhere([
                'or',
                ['type' => $this->type],
                ['type' => 'commit'],
                ['type' => 'close'],
            ]);
            $query->andWhere([
                '!=', 'system_notice_id', 0,
            ]);
            return $query;
        }
        $query->andWhere([
            'user_id' => $this->user_id,
        ]);
//        if ($this->type == 'notice') { // 收到的通告（邀请）
//            $query->andWhere([
//                'and',
//                ['!=', 'notice_id', 0],
//                ['actor_id' => 0],
//
//            ]);
//        } elseif ($this->type == 'answer') { // 应邀的通告（简历）
//            $query->andWhere([
//                'and',
//                ['!=', 'notice_id', 0],
//                ['!=', 'actor_id', 0],
//
//            ]);
//        } elseif ($this->type == 'resume') { // 投简历
//            $query->andWhere([
//                'and',
//                ['notice_id' => 0],
//                ['!=', 'actor_id', 0],
//            ]);
//        }
        $query->andWhere([
            'and',
            ['type' => $this->type],
        ]);
//        Yii::error($query->debug(), 'dddddd');
        return $query;
    }

    public function fields()
    {
        return [
            'id',
            'user_id',
            'title',
            'description',
            'sender',
            'notice_id',
            'actor_id',
            'system_notice_id',
            'status',
            'type' => function () {
                return parent::getAttribute('type');
            },
            'created_at',
            'updated_at',
        ];
    }
}